package com.cheer.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.DocumentTemplateDao;
import com.cheer.dao.StandingBookDocumentDao;
import com.cheer.dto.MemberImportDTO;
import com.cheer.entity.DocumentTemplate;
import com.cheer.entity.StandingBookDocument;
import com.cheer.exception.ServiceException;
import com.cheer.properties.DocumentProperties;
import com.cheer.service.StandingBookDocumentService;
import com.cheer.util.FileUtil;
import com.cheer.vo.DocumentMemberVO;
import com.cheer.vo.DocumentVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


/**
 * @author cheer
 */
@Service
@Slf4j
public class StandingBookDocumentServiceImpl extends ServiceImpl<StandingBookDocumentDao, StandingBookDocument>
        implements StandingBookDocumentService {

    @Autowired
    private DocumentProperties documentProperties;

    @Autowired
    private DocumentTemplateDao documentTemplateDao;

    @Override
    public String getTemplateByType(Integer type) {
        DocumentTemplate documentTemplate = documentTemplateDao.getByType(type);
        if (documentTemplate == null) {
            return null;
        }
        return documentProperties.getUrl() + documentTemplate.getDocUrl();
    }

    @Override
    public DocumentMemberVO uploadMemberDocument(Long accountId, MultipartFile file) {
        // 校验文件
        FileUtil.checkFile(file, "xlsx");
        // 存储文件
        String fileName = "成员基本情况.xlsx";
        String fileUrl = File.separator + accountId + File.separator + UUID.randomUUID().toString() + File.separator + fileName;
        String filePath = documentProperties.getPath() + fileUrl;
        FileUtil.saveFile(file, filePath);
        // 读取文件
        List<MemberImportDTO> members;
        try (ExcelReader reader = ExcelUtil.getReader(filePath)) {
            reader.setHeaderAlias(MemberImportDTO.getHeaderAlias());
            members = reader.read(0, 1, MemberImportDTO.class);
        }catch (Exception exception){
            log.info("文件列转化异常！", exception);
            throw new ServiceException("上传文件无效，请检查文件内容");
        }
        // 验证文件数据，计算年龄
        if (CollectionUtil.isEmpty(members)) {
            throw new ServiceException("上传文件无效，请检查文件内容");
        }
        for (int i = 0; i < members.size(); i++) {
            members.get(i).checkAndCompute(i+1);
        }
        // 返回数据处理
        return DocumentMemberVO.build(fileName, fileUrl, documentProperties.getUrl(), members);
    }

    @Override
    public DocumentVO upload(Long accountId, MultipartFile file) {
        if (file == null) {
            throw new ServiceException("上传的文件无效");
        }
        // 存储文件
        String fileName = file.getOriginalFilename();

        String fileUrl = "";
        if(ObjectUtil.isNotNull(accountId)){
            fileUrl = File.separator + accountId + File.separator + UUID.randomUUID().toString() + File.separator + fileName;
        }else{
            // 标识注册时上传佐证文件
            fileUrl = File.separator + "register" + File.separator + UUID.randomUUID().toString() + File.separator + fileName;
        }

        FileUtil.saveFile(file, documentProperties.getPath() + fileUrl);
        // 返回数据处理
        return DocumentVO.build(fileName, fileUrl, documentProperties.getUrl());
    }


}
